//
//  MeViewController.m
//  HWZB
//
//  Created by 许成雄 on 2017/4/13.
//  Copyright © 2017年 wonders. All rights reserved.
//

#import "MeViewController.h"
#import "SettingItemInfo.h"
#import "SettingCollectionViewCell.h"
#import "AppDelegate.h"

#import "QRCodeViewController.h"
#import "IGetViewController.h"
#import "WithdrawViewController.h"
#import "WithdrawRecordViewController.h"
#import "SettingViewController.h"
#import "UserInfoViewController.h"
#import "AccountInfoViewController.h"

#import "HWZBAPIRequest.h"
#import <SVProgressHUD/SVProgressHUD.h>
#import "UIImageView+Webcache.h"
#import "MBProgressHUD+Toast.h"
#import "Context.h"

@interface MeViewController () <UICollectionViewDelegate, UICollectionViewDataSource>

@property (strong, nonatomic) UIButton *userInfoView;
@property (strong, nonatomic) UIButton *accountInfoView;
@property (strong, nonatomic) UIButton *teamInfoView;

@property (strong, nonatomic) UICollectionView *collectionView;
@property (strong, nonatomic) NSMutableArray *itemArray;

@end

@implementation MeViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    // Do any additional setup after loading the view.
    self.edgesForExtendedLayout = UIRectEdgeNone;
    self.view.backgroundColor = I_COLOR_BACKGROUND;
    self.navigationItem.title = @"个人中心";
    
    [self createUI];
    [self loadData];
}

- (void)registerNotifications {
    //用户登录(退出登录)通知
    [[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(userLoginNotifyAction:) name:kNotificationUserLogin object:nil];
    [[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(userInfoModifyNotifyAction:) name:kNotificationUserInfoModify object:nil];
    [[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(accountInfoChangeNotifyAction:) name:kNotificationAccountInfoChange object:nil];
}

- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

#pragma mark - UIButtonAciton
- (void)userLoginNotifyAction:(NSNotification *)notification {
    //刷新数据
    [self loadData];
}

- (void)userInfoModifyNotifyAction:(NSNotification *)notification {
    //获取用户信息
    [self getUserInfo];
}

- (void)accountInfoChangeNotifyAction:(NSNotification *)notification {
    //获取账户信息
    [self getAccountInfo];
}


- (void)userInfoTapAction:(id)sender {
    BOOL hasLogin = [Context sharedInstance].isLogin;
    if(!hasLogin) {
        //用户未登录
        [[AppDelegate sharedDelegate] showLoginViewController];
    } else {
        //用户已登录
        UserInfoViewController *userInfoViewController = [[UserInfoViewController alloc] init];
        userInfoViewController.hidesBottomBarWhenPushed = YES;
        [self.navigationController pushViewController:userInfoViewController animated:YES];
    }
}

- (void)accountInfoTapAction:(id)sender {
    BOOL hasLogin = [Context sharedInstance].isLogin;
    if(!hasLogin) {
        //用户未登录
        [[AppDelegate sharedDelegate] showLoginViewController];
    } else {
        //用户已登录
        AccountInfoViewController *accountInfoViewController = [[AccountInfoViewController alloc] init];
        accountInfoViewController.hidesBottomBarWhenPushed = YES;
        [self.navigationController pushViewController:accountInfoViewController animated:YES];
    }
}

- (void)teamInfoTapAction:(id)sender {
    //团队信息
    return;
}

#pragma mark - Private Method
- (void)getUserInfo {
    NSString *loginId = [Context sharedInstance].loginID;
    NSMutableDictionary *params = [NSMutableDictionary dictionaryWithCapacity:0];
    if(loginId != nil) {
        [params setObject:loginId forKey:@"id"];
    }
    [SVProgressHUD showWithStatus:@"正在获取用户信息..." maskType:SVProgressHUDMaskTypeClear];
    [[HWZBAPIRequest sharedInstance] getUserInfoWithParams:params success:^(UserInfo *userInfo) {
        [SVProgressHUD dismiss];
        if(userInfo) {
            [Context sharedInstance].userInfo = userInfo;
            [self showUserInfo:userInfo];
        } else {
            [Context sharedInstance].userInfo = nil;
            [self showUserInfo:nil];
        }
    } failure:^(NSError *error) {
        [SVProgressHUD dismiss];
        [Context sharedInstance].userInfo = nil;
        [self showUserInfo:nil];
    }];
}

- (void)getAccountInfo {
    NSMutableDictionary *params = [NSMutableDictionary dictionaryWithCapacity:0];
    [SVProgressHUD showWithStatus:@"正在获取账户信息..." maskType:SVProgressHUDMaskTypeClear];
    [[HWZBAPIRequest sharedInstance] getAccountInfoWithParams:params success:^(AccountInfo *accountInfo) {
        [SVProgressHUD dismiss];
        if(accountInfo) {
            [Context sharedInstance].accountInfo = accountInfo;
            [self showAccountInfo:accountInfo];
        } else {
            [Context sharedInstance].accountInfo = nil;
            [self showAccountInfo:nil];
        }
    } failure:^(NSError *error) {
        [SVProgressHUD dismiss];
        [Context sharedInstance].accountInfo = nil;
        [self showAccountInfo:nil];
    }];

}

- (void)getTeamInfo {
    NSMutableDictionary *params = [NSMutableDictionary dictionaryWithCapacity:0];
    [SVProgressHUD showWithStatus:@"正在获取团队信息..." maskType:SVProgressHUDMaskTypeClear];
    [[HWZBAPIRequest sharedInstance] getMyTeamInfoWithParams:params success:^(TeamInfo *teamInfo) {
        [SVProgressHUD dismiss];
        if(teamInfo) {
            [self showTeamInfo:teamInfo];
        } else {
            [self showTeamInfo:nil];
        }
    } failure:^(NSError *error) {
        [SVProgressHUD dismiss];
        [self showTeamInfo:nil];
    }];
    
}

#pragma mark - UICollectionViewDelegate && UICollectionViewDatesource
- (NSInteger)numberOfSectionsInCollectionView:(UICollectionView *)collectionView {
    return 1;
}

- (NSInteger)collectionView:(UICollectionView *)collectionView numberOfItemsInSection:(NSInteger)section {
    NSInteger count = self.itemArray != nil ? [self.itemArray count] : 0;
    return count;
}

-(CGSize)collectionView:(UICollectionView *)collectionView layout:(UICollectionViewLayout *)collectionViewLayout sizeForItemAtIndexPath:(NSIndexPath *)indexPath{
    CGFloat width = SCREEN_WIDTH / 3;
    CGFloat height = width;
    return CGSizeMake(width, height);
}

- (UICollectionViewCell *)collectionView:(UICollectionView *)collectionView cellForItemAtIndexPath:(NSIndexPath *)indexPath {
    SettingItemInfo *settingItemInfo = nil;
    if(indexPath.row < [self.itemArray count]) {
        settingItemInfo = [self.itemArray objectAtIndex:indexPath.row];
    }
    
    SettingCollectionViewCell *cell = [collectionView dequeueReusableCellWithReuseIdentifier:@"SettingCollectionViewCell" forIndexPath:indexPath];
    if(!cell) {
        CGFloat width = SCREEN_WIDTH / 3;
        CGFloat height = width;
        cell = [[SettingCollectionViewCell alloc] initWithFrame:CGRectMake(0, 0, width, height)];
    }
    cell.backgroundColor = [UIColor clearColor];
    
    if(settingItemInfo) {
        cell.settingItemInfo = settingItemInfo;
    } else {
        cell.hidden = YES;
    }
    
    return cell;
}

- (UIEdgeInsets)collectionView:(UICollectionView *)collectionView layout:(UICollectionViewLayout*)collectionViewLayout insetForSectionAtIndex:(NSInteger)section {
    UIEdgeInsets inset = UIEdgeInsetsMake(0, 0, 0, 0);
    return inset;
}

- (CGFloat)collectionView:(UICollectionView *)collectionView layout:(UICollectionViewLayout*)collectionViewLayout minimumLineSpacingForSectionAtIndex:(NSInteger)section {
    return 0.0f;
}

- (CGFloat)collectionView:(UICollectionView *)collectionView layout:(UICollectionViewLayout*)collectionViewLayout minimumInteritemSpacingForSectionAtIndex:(NSInteger)section {
    return 0.0f;
}

- (void)collectionView:(UICollectionView *)collectionView didSelectItemAtIndexPath:(NSIndexPath *)indexPath {
    //TODO--跳转到词条列表
    BOOL hasLogin = [Context sharedInstance].isLogin;
    if(!hasLogin) {
        //用户未登录
        [[AppDelegate sharedDelegate] showLoginViewController];
        return;
    } else {
        //用户已登录
        if(indexPath.row == 0) {
            QRCodeViewController *qrCodeViewController = [[QRCodeViewController alloc] init];
            qrCodeViewController.hidesBottomBarWhenPushed = YES;
            [self.navigationController pushViewController:qrCodeViewController animated:YES];
        } else if(indexPath.row == 1) {
            //TODO -- 我要领取
            [self iGetAction];
        } else if(indexPath.row == 2) {
            WithdrawViewController *withdrawViewController = [[WithdrawViewController alloc] init];
            withdrawViewController.hidesBottomBarWhenPushed = YES;
            [self.navigationController pushViewController:withdrawViewController animated:YES];
        } else if(indexPath.row == 3) {
            WithdrawRecordViewController *withdrawRecordViewController = [[WithdrawRecordViewController alloc] init];
            withdrawRecordViewController.hidesBottomBarWhenPushed = YES;
            [self.navigationController pushViewController:withdrawRecordViewController animated:YES];
        } else if(indexPath.row == 4) {
            SettingViewController *settingViewController = [[SettingViewController alloc] init];
            settingViewController.hidesBottomBarWhenPushed = YES;
            [self.navigationController pushViewController:settingViewController animated:YES];
        } else {
            //待定
        }
    }
}

- (BOOL)collectionView:(UICollectionView *)collectionView shouldSelectItemAtIndexPath:(NSIndexPath *)indexPath {
    return YES;
}

- (void)iGetAction {
    NSMutableDictionary *params = [NSMutableDictionary dictionaryWithCapacity:0];
    [SVProgressHUD showWithStatus:@"正在领取..." maskType:SVProgressHUDMaskTypeClear];
    [[HWZBAPIRequest sharedInstance] getIGetInfoWithParams:params success:^(CommonInfo *resultInfo) {
        [SVProgressHUD dismiss];
        if(resultInfo && [resultInfo isSuccess]) {
            [MBProgressHUD showToast:@"领取成功" initWithView:self.view];
        } else {
            [MBProgressHUD showToast:@"领取失败" initWithView:self.view];
        }
    } failure:^(NSError *error) {
        [SVProgressHUD dismiss];
        [MBProgressHUD showToast:[error localizedDescription] initWithView:self.view];
    }];
}

#pragma mark - Private Method
- (void)createUI {
    self.userInfoView = [self createUserInfoView];
    self.accountInfoView = [self createAccountInfoView];
    self.teamInfoView = [self createTeamInfoView];
    
    
    [self.view addSubview:self.userInfoView];
    [self.view addSubview:self.accountInfoView];
    [self.view addSubview:self.teamInfoView];
    
    [self.userInfoView addTarget:self action:@selector(userInfoTapAction:) forControlEvents:UIControlEventTouchUpInside];
    
    [self.accountInfoView addTarget:self action:@selector(accountInfoTapAction:) forControlEvents:UIControlEventTouchUpInside];
    
    [self.teamInfoView addTarget:self action:@selector(teamInfoTapAction:) forControlEvents:UIControlEventTouchUpInside];
    
    //实例化表单控件
    UICollectionViewFlowLayout *layout = [[UICollectionViewFlowLayout alloc] init];
    self.collectionView = [[UICollectionView alloc] initWithFrame:CGRectMake(0, TRANS_VALUE(240.0f), SCREEN_WIDTH, SCREEN_HEIGHT - 64 - 49 - TRANS_VALUE(240.0f)) collectionViewLayout:layout];
    [self.view addSubview:self.collectionView];
    self.collectionView.backgroundColor = I_COLOR_WHITE;
    self.collectionView.showsVerticalScrollIndicator = NO;
    
    self.collectionView.delegate = self;
    self.collectionView.dataSource = self;
    
    [self.collectionView registerClass:[SettingCollectionViewCell class] forCellWithReuseIdentifier:@"SettingCollectionViewCell"];
    
}

- (UIButton *)createUserInfoView {
    UIButton *button = [[UIButton alloc] initWithFrame:CGRectMake(0, 0, SCREEN_WIDTH, TRANS_VALUE(100.0f))];
    button.backgroundColor = I_COLOR_WHITE;
    [button setBackgroundImage:[UIImage imageNamed:@"bg_user_info"] forState:UIControlStateNormal];
    [button setBackgroundImage:[UIImage imageNamed:@"bg_user_info"] forState:UIControlStateSelected];
    [button setBackgroundImage:[UIImage imageNamed:@"bg_user_info"] forState:UIControlStateHighlighted];
    UIImageView *avatarImageView = [[UIImageView alloc] initWithFrame:CGRectMake(TRANS_VALUE(10.0f), TRANS_VALUE(20.0f), TRANS_VALUE(60.0f), TRANS_VALUE(60.0f))];
    avatarImageView.contentMode = UIViewContentModeScaleAspectFit;
    avatarImageView.clipsToBounds = YES;
    avatarImageView.layer.cornerRadius = TRANS_VALUE(60.0f) / 2;
    avatarImageView.layer.borderWidth = TRANS_VALUE(0.0f);
    [button addSubview:avatarImageView];
    avatarImageView.image = [UIImage imageNamed:@"ic_avatar_default"];
    [avatarImageView setTag:10000];
    
    UILabel *userNameLabel = [[UILabel alloc] initWithFrame:CGRectMake(TRANS_VALUE(80.0f), TRANS_VALUE(30.0f), TRANS_VALUE(100.0f), TRANS_VALUE(20.0f))];
    userNameLabel.textColor = I_COLOR_WHITE;
    userNameLabel.font = [UIFont systemFontOfSize:TRANS_VALUE(12.0f)];
    [button addSubview:userNameLabel];
    userNameLabel.text = @"张三";
    [userNameLabel setTag:10001];
    
    UILabel *phoneNumberLabel = [[UILabel alloc] initWithFrame:CGRectMake(TRANS_VALUE(80.0f), TRANS_VALUE(50.0f), TRANS_VALUE(100.0f), TRANS_VALUE(20.0f))];
    phoneNumberLabel.textColor = I_COLOR_WHITE;
    phoneNumberLabel.font = [UIFont systemFontOfSize:TRANS_VALUE(12.0f)];
    [button addSubview:phoneNumberLabel];
    phoneNumberLabel.text = @"18016023429";
    [phoneNumberLabel setTag:10002];
    
    UIImageView *markImageView = [[UIImageView alloc] initWithFrame:CGRectMake(TRANS_VALUE(200.0f), TRANS_VALUE(40.0f), TRANS_VALUE(20.0f), TRANS_VALUE(20.0f))];
    markImageView.contentMode = UIViewContentModeScaleAspectFit;
    markImageView.layer.borderColor = [UIColor colorWithWhite:1.0f alpha:0.5].CGColor;
    [button addSubview:markImageView];
    markImageView.image = [UIImage imageNamed:@"ic_me_certificate_mark"];
    [markImageView setTag:10003];
    
    UILabel *markLabel = [[UILabel alloc] initWithFrame:CGRectMake(TRANS_VALUE(224.0f), TRANS_VALUE(40.0f), TRANS_VALUE(100.0f), TRANS_VALUE(20.0f))];
    markLabel.textColor = I_COLOR_WHITE;
    markLabel.font = [UIFont systemFontOfSize:TRANS_VALUE(12.0f)];
    [button addSubview:markLabel];
    markLabel.text = @"已认证";
    [markLabel setTag:10004];
    
    UILabel *loginLabel = [[UILabel alloc] initWithFrame:CGRectMake(TRANS_VALUE(80.0f), TRANS_VALUE(40.0f), TRANS_VALUE(100.0f), TRANS_VALUE(20.0f))];
    loginLabel.textColor = I_COLOR_WHITE;
    loginLabel.font = [UIFont systemFontOfSize:TRANS_VALUE(14.0f)];
    [button addSubview:loginLabel];
    loginLabel.text = @"登录/注册";
    [loginLabel setTag:10005];
    
    UIImageView *arrowImageView = [[UIImageView alloc] initWithFrame:CGRectMake(TRANS_VALUE(300.0f), TRANS_VALUE(44.0f), TRANS_VALUE(7.0f), TRANS_VALUE(12.0f))];
    arrowImageView.contentMode = UIViewContentModeScaleAspectFit;
    [button addSubview:arrowImageView];
    arrowImageView.image = [UIImage imageNamed:@"ic_me_arrow_white"];

    return button;
}

//未登录时的用户信息
- (void)showUserInfoWithoutLogin {
    UIImageView *avatarImageView = (UIImageView *)[self.userInfoView viewWithTag:10000];
    UILabel *userNameLabel = (UILabel *)[self.userInfoView viewWithTag:10001];
    UILabel *mobilePhoneLabel = (UILabel *)[self.userInfoView viewWithTag:10002];
    UIImageView *markImageVeiw = (UIImageView *)[self.userInfoView viewWithTag:10003];
    UILabel *markLabel = (UILabel *)[self.userInfoView viewWithTag:10004];
    UILabel *loginLabel = (UILabel *)[self.userInfoView viewWithTag:10005];
    avatarImageView.hidden = NO;
    mobilePhoneLabel.hidden = YES;
    userNameLabel.hidden = YES;
    markLabel.hidden = YES;
    markImageVeiw.hidden = YES;
    loginLabel.hidden = NO;
    [Context sharedInstance].userInfo = nil;
}


- (void)showUserInfo:(UserInfo *)userInfo {
    UIImageView *avatarImageView = (UIImageView *)[self.userInfoView viewWithTag:10000];
    UILabel *userNameLabel = (UILabel *)[self.userInfoView viewWithTag:10001];
    UILabel *mobilePhoneLabel = (UILabel *)[self.userInfoView viewWithTag:10002];
    UIImageView *markImageVeiw = (UIImageView *)[self.userInfoView viewWithTag:10003];
    UILabel *markLabel = (UILabel *)[self.userInfoView viewWithTag:10004];
    UILabel *loginLabel = (UILabel *)[self.userInfoView viewWithTag:10005];
    mobilePhoneLabel.hidden = NO;
    userNameLabel.hidden = NO;
    markLabel.hidden = NO;
    markImageVeiw.hidden = NO;
    loginLabel.hidden = YES;
    //用户已登录
    if(userInfo) {
        [avatarImageView sd_setImageWithURL:[NSURL URLWithString:userInfo.imageUrl] placeholderImage:[UIImage imageNamed:@"ic_avatar_default"]];
        userNameLabel.text = userInfo.name;
        mobilePhoneLabel.text = userInfo.mobile;
        if([userInfo isAuthenticated]) {
            markImageVeiw.hidden = NO;
            markLabel.text = @"已认证";
        } else {
            markImageVeiw.hidden = YES;
            markLabel.text = @"未认证";
        }
    } else {
        avatarImageView.image = [UIImage imageNamed:@"ic_avatar_default"];
        userNameLabel.text = @"--";
        mobilePhoneLabel.text = @"----";
        markImageVeiw.hidden = YES;
        markLabel.text = @"未认证";
    }

}

- (UIButton *)createAccountInfoView {
    UIButton *button = [[UIButton alloc] initWithFrame:CGRectMake(0, TRANS_VALUE(100.0f), SCREEN_WIDTH, TRANS_VALUE(64.0f))];
    button.backgroundColor = I_COLOR_WHITE;
    UIImageView *accountImageView = [[UIImageView alloc] initWithFrame:CGRectMake(TRANS_VALUE(24.0f), TRANS_VALUE(16.0f), TRANS_VALUE(16.0f), TRANS_VALUE(16.0f))];
    accountImageView.contentMode = UIViewContentModeScaleAspectFit;
    accountImageView.image = [UIImage imageNamed:@"ic_me_account"];
    [button addSubview:accountImageView];
    
    UILabel *accountLabel = [[UILabel alloc] initWithFrame:CGRectMake(TRANS_VALUE(0.0f), TRANS_VALUE(32.0f), TRANS_VALUE(64.0f), TRANS_VALUE(20.0f))];
    accountLabel.textAlignment = NSTextAlignmentCenter;
    accountLabel.textColor = I_COLOR_BLACK;
    accountLabel.font = [UIFont boldSystemFontOfSize:TRANS_VALUE(11.0f)];
    accountLabel.text = @"账户中心";
    [button addSubview:accountLabel];
    
    UIImageView *dividerView = [[UIImageView alloc] initWithFrame:CGRectMake(TRANS_VALUE(72.0f), TRANS_VALUE(0.0f), TRANS_VALUE(7.0f), TRANS_VALUE(64.0f))];
    dividerView.contentMode = UIViewContentModeScaleAspectFit;
    dividerView.image = [UIImage imageNamed:@"ic_me_divider"];
    [button addSubview:dividerView];
    
    UILabel *diamondLabel = [[UILabel alloc] initWithFrame:CGRectMake(TRANS_VALUE(80.0f), TRANS_VALUE(0.0f), TRANS_VALUE(73.0f), TRANS_VALUE(64.0f))];
    diamondLabel.font = [UIFont systemFontOfSize:TRANS_VALUE(11.0f)];
    diamondLabel.textColor = I_COLOR_BLACK;
    [button addSubview:diamondLabel];
    diamondLabel.textAlignment = NSTextAlignmentCenter;
    diamondLabel.text = @"--\n\n钻石苹果";
    diamondLabel.numberOfLines = 0;
    [diamondLabel setTag:20000];
    
    UILabel *goldLabel = [[UILabel alloc] initWithFrame:CGRectMake(TRANS_VALUE(153.0f), TRANS_VALUE(0.0f), TRANS_VALUE(73.0f), TRANS_VALUE(64.0f))];
    goldLabel.font = [UIFont systemFontOfSize:TRANS_VALUE(11.0f)];
    goldLabel.textColor = I_COLOR_BLACK;
    [button addSubview:goldLabel];
    goldLabel.textAlignment = NSTextAlignmentCenter;
    goldLabel.text = @"4000\n\n金苹果";
    goldLabel.numberOfLines = 0;
    [goldLabel setTag:20001];
    
    UILabel *silverLabel = [[UILabel alloc] initWithFrame:CGRectMake(TRANS_VALUE(226.0f), TRANS_VALUE(0.0f), TRANS_VALUE(74.0f), TRANS_VALUE(64.0f))];
    silverLabel.font = [UIFont systemFontOfSize:TRANS_VALUE(11.0f)];
    silverLabel.textColor = I_COLOR_BLACK;
    [button addSubview:silverLabel];
    silverLabel.textAlignment = NSTextAlignmentCenter;
    silverLabel.text = @"2000\n\n银苹果";
    silverLabel.numberOfLines = 0;
    [silverLabel setTag:20002];
    
    UIImageView *arrowImageView = [[UIImageView alloc] initWithFrame:CGRectMake(TRANS_VALUE(300.0f), TRANS_VALUE(27.0f), TRANS_VALUE(6.0f), TRANS_VALUE(10.0f))];
    arrowImageView.contentMode = UIViewContentModeScaleAspectFit;
    arrowImageView.image = [UIImage imageNamed:@"ic_me_arrow_black"];
    [button addSubview:arrowImageView];
    
    return button;
}

- (void)showAccountInfoWithoutLogin {
    UILabel *diamonLabel = (UILabel *)[self.accountInfoView viewWithTag:20000];
    UILabel *goldLabel = (UILabel *)[self.accountInfoView viewWithTag:20001];
    UILabel *silverLabel = (UILabel *)[self.accountInfoView viewWithTag:20002];
    diamonLabel.text = [NSString stringWithFormat:@"%@\n\n钻石苹果", @"0"];
    goldLabel.text = [NSString stringWithFormat:@"%@\n\n金苹果", @"0"];
    silverLabel.text = [NSString stringWithFormat:@"%@\n\n银苹果", @"0"];
    [Context sharedInstance].accountInfo = nil;
   
}

- (void)showAccountInfo:(AccountInfo *)accountInfo {
    UILabel *diamonLabel = (UILabel *)[self.accountInfoView viewWithTag:20000];
    UILabel *goldLabel = (UILabel *)[self.accountInfoView viewWithTag:20001];
    UILabel *silverLabel = (UILabel *)[self.accountInfoView viewWithTag:20002];
    if(accountInfo) {
        diamonLabel.text = [NSString stringWithFormat:@"%@\n\n钻石苹果", accountInfo.diamondAccount];
        goldLabel.text = [NSString stringWithFormat:@"%@\n\n金苹果", accountInfo.goldenAccount];
        silverLabel.text = [NSString stringWithFormat:@"%@\n\n银苹果", accountInfo.silverAccount];
    } else {
        diamonLabel.text = [NSString stringWithFormat:@"%@\n\n钻石苹果", @"0"];
        goldLabel.text = [NSString stringWithFormat:@"%@\n\n金苹果",  @"0"];
        silverLabel.text = [NSString stringWithFormat:@"%@\n\n银苹果",  @"0"];
    }
}

- (UIButton *)createTeamInfoView {
     UIButton *button = [[UIButton alloc] initWithFrame:CGRectMake(0, TRANS_VALUE(164.0f) + TRANS_VALUE(6.0f), SCREEN_WIDTH, TRANS_VALUE(64.0f))];
    button.backgroundColor = I_COLOR_WHITE;
    UIImageView *accountImageView = [[UIImageView alloc] initWithFrame:CGRectMake(TRANS_VALUE(24.0f), TRANS_VALUE(16.0f), TRANS_VALUE(16.0f), TRANS_VALUE(16.0f))];
    accountImageView.contentMode = UIViewContentModeScaleAspectFit;
    accountImageView.image = [UIImage imageNamed:@"ic_me_team"];
    [button addSubview:accountImageView];
    
    UILabel *accountLabel = [[UILabel alloc] initWithFrame:CGRectMake(TRANS_VALUE(0.0f), TRANS_VALUE(32.0f), TRANS_VALUE(64.0f), TRANS_VALUE(20.0f))];
    accountLabel.textAlignment = NSTextAlignmentCenter;
    accountLabel.textColor = I_COLOR_BLACK;
    accountLabel.font = [UIFont boldSystemFontOfSize:TRANS_VALUE(11.0f)];
    accountLabel.text = @"我的团队";
    [button addSubview:accountLabel];
    
    UIImageView *dividerView = [[UIImageView alloc] initWithFrame:CGRectMake(TRANS_VALUE(72.0f), TRANS_VALUE(0.0f), TRANS_VALUE(7.0f), TRANS_VALUE(64.0f))];
    dividerView.contentMode = UIViewContentModeScaleAspectFit;
    dividerView.image = [UIImage imageNamed:@"ic_me_divider"];
    [button addSubview:dividerView];
    
    UILabel *firstLabel = [[UILabel alloc] initWithFrame:CGRectMake(TRANS_VALUE(80.0f), TRANS_VALUE(0.0f), TRANS_VALUE(115.0f), TRANS_VALUE(64.0f))];
    firstLabel.font = [UIFont systemFontOfSize:TRANS_VALUE(11.0f)];
    firstLabel.textColor = I_COLOR_BLACK;
    firstLabel.numberOfLines = 0;
    [button addSubview:firstLabel];
    firstLabel.textAlignment = NSTextAlignmentCenter;
    firstLabel.text = @"20\n\n一级团队";
    [firstLabel setTag:30001];
    
    UILabel *secondLabel = [[UILabel alloc] initWithFrame:CGRectMake(TRANS_VALUE(195.0f), TRANS_VALUE(0.0f), TRANS_VALUE(115.0f), TRANS_VALUE(64.0f))];
    secondLabel.font = [UIFont systemFontOfSize:TRANS_VALUE(11.0f)];
    secondLabel.textColor = I_COLOR_BLACK ;
    [button addSubview:secondLabel];
    secondLabel.numberOfLines = 0;
    secondLabel.textAlignment = NSTextAlignmentCenter;
    secondLabel.text = @"20\n\n二级团队";
    [secondLabel setTag:30000];
    
    return button;
}

- (void)showTeamInfoWithoutLogin {
    UILabel *secondLabel = (UILabel *)[self.teamInfoView viewWithTag:30000];
    UILabel *firstLabel = (UILabel *)[self.teamInfoView viewWithTag:30001];
    secondLabel.text = [NSString stringWithFormat:@"%@\n\n二级团队", @"0"];
    firstLabel.text = [NSString stringWithFormat:@"%@\n\n一级团队", @"0"];
}

- (void)showTeamInfo:(TeamInfo *)teamInfo {
    UILabel *secondLabel = (UILabel *)[self.teamInfoView viewWithTag:30000];
    UILabel *firstLabel = (UILabel *)[self.teamInfoView viewWithTag:30001];
    if(teamInfo) {
        secondLabel.text = [NSString stringWithFormat:@"%@\n\n二级团队", teamInfo.twoLevel];
        firstLabel.text = [NSString stringWithFormat:@"%@\n\n一级团队", teamInfo.oneLevel];
    } else {
        secondLabel.text = [NSString stringWithFormat:@"%@\n\n二级团队", @"0"];
        firstLabel.text = [NSString stringWithFormat:@"%@\n\n一级团队",  @"0"];
    }
}

- (void)loadData {
    BOOL hasLogin = [Context sharedInstance].isLogin;
    if(hasLogin) {
        //用户已登录
        [self getUserInfo];
        [self getAccountInfo];
        [self getTeamInfo];
    } else {
        //用户未登录
        [self showUserInfoWithoutLogin];
        [self showAccountInfoWithoutLogin];
        [self showTeamInfoWithoutLogin];
    }
    [self getSettingInfo];
}

- (void)getSettingInfo {
    if(!self.itemArray) {
        self.itemArray = [[NSMutableArray alloc] initWithCapacity:0];
    }
    [self.itemArray removeAllObjects];
    SettingItemInfo *settingItem0 = [[SettingItemInfo alloc] init];
    settingItem0.icon = @"ic_me_qrcode";
    settingItem0.title = @"我的二维码";
    [self.itemArray addObject:settingItem0];
    SettingItemInfo *settingItem1 = [[SettingItemInfo alloc] init];
    settingItem1.icon = @"ic_me_iget";
    settingItem1.title = @"我要领取";
    [self.itemArray addObject:settingItem1];
    SettingItemInfo *settingItem2 = [[SettingItemInfo alloc] init];
    settingItem2.icon = @"ic_me_withdraw";
    settingItem2.title = @"我要提现";
    [self.itemArray addObject:settingItem2];
    SettingItemInfo *settingItem3 = [[SettingItemInfo alloc] init];
    settingItem3.icon = @"ic_me_withdraw_record";
    settingItem3.title = @"提现记录";
    [self.itemArray addObject:settingItem3];
    SettingItemInfo *settingItem4 = [[SettingItemInfo alloc] init];
    settingItem4.icon = @"ic_me_setting";
    settingItem4.title = @"设置";
    [self.itemArray addObject:settingItem4];
    
    [self.collectionView reloadData];
}

/*
#pragma mark - Navigation

// In a storyboard-based application, you will often want to do a little preparation before navigation
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
    // Get the new view controller using [segue destinationViewController].
    // Pass the selected object to the new view controller.
}
*/

@end
